package com.unionftech.common.common.adapter

import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.unionftech.common.base.BaseFragment

class CommonFragmentStateAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
    private val fragmentList = SparseArray<BaseFragment>()

    fun addFragment(fragments: List<BaseFragment>) {
        fragmentList.clear()
        fragments.forEachIndexed { index, fragment ->
            fragmentList.put(index, fragment)
        }
    }

    fun getFragment(position: Int): BaseFragment? {
        return fragmentList.get(position)
    }

    override fun getItemCount(): Int {
        return fragmentList.size()
    }

    override fun createFragment(position: Int): Fragment {
        return fragmentList.get(position)
    }
}